package com.blog.backend.service.impl;

import com.blog.backend.exception.GlobalException;
import com.blog.backend.mapper.BlogPlateMapper;
import com.blog.backend.model.BlogPlate;
import com.blog.backend.req.EditPlateReq;
import com.blog.backend.req.InsertPlateReq;
import com.blog.backend.req.PlateListReq;
import com.blog.backend.resp.ClassifyResp;
import com.blog.backend.service.ClassifyTagService;
import com.blog.backend.service.PlateService;
import com.blog.backend.config.CodeMsg;
import com.blog.backend.utils.PageUtils;
import com.blog.backend.utils.UuidUtils;
import com.blog.backend.vo.PageRequest;
import com.blog.backend.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author fanggs
 * @Date 2021/7/14 0014 10:01
 */
@Slf4j
@Service
public class PlateServiceImpl implements PlateService {

    @Resource
    private BlogPlateMapper blogPlateMapper;

    @Resource
    private ClassifyTagService classifyTagService;

    @Override
    public Boolean insertPlate(InsertPlateReq req) {
        log.info("新增板块分类:{}",req.getPlateName());
        Integer count=blogPlateMapper.selectByPlateName(req.getPlateName());
        if (count>0){
            throw new GlobalException(CodeMsg.PLATE_NAME_EXIST);
        }
        BlogPlate blogPlate = new BlogPlate();
        blogPlate.setPlateId(UuidUtils.getId());
        blogPlate.setPlateName(req.getPlateName());
        blogPlate.setPlateDescription(req.getPlateDescription());
        blogPlate.setType(req.getType());
        int i = blogPlateMapper.insertSelective(blogPlate);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public PageResult selectList(PlateListReq req) {
        log.info("板块列表查询:{}",req);
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<BlogPlate> list = blogPlateMapper.selectList(req);
        PageInfo info=new PageInfo<BlogPlate>(list);
        PageRequest pageRequest = new PageRequest();
        PageResult pageResult = PageUtils.getPageResult(pageRequest, info);
        return pageResult;
    }

    @Override
    public BlogPlate queryDetail(String plateId) {
        log.info("板块模块详情:{}",plateId);
        BlogPlate plate = blogPlateMapper.selectByPrimaryKey(plateId);
        if (ObjectUtils.isEmpty(plate)){
            throw new GlobalException(CodeMsg.PLATE_INFO_ERROR);
        }
        return plate;
    }

    @Override
    public Integer editDetail(EditPlateReq req) {
        log.info("修改板块内容:{}",req.getPlateId());
        BlogPlate plate=blogPlateMapper.selectDetailByName(req.getPlateName());
        if (plate!=null && !plate.getPlateId().equals(req.getPlateId())){
            log.info("查询到的板块分类id:{}",plate.getPlateId());
            throw new GlobalException(CodeMsg.PLATE_NAME_REPETITION);
        }
        BlogPlate newPlate = new BlogPlate();
        newPlate.setPlateId(req.getPlateId());
        newPlate.setPlateName(req.getPlateName());
        newPlate.setPlateDescription(req.getPlateDescription());
        newPlate.setUpdateTime(new Date());
        Integer i = blogPlateMapper.updateByPrimaryKeySelective(newPlate);
        if (i>0){
            return i;
        }
        return 0;
    }

    @Override
    public Integer deletePlate(String plateId) {
        log.info("删除板块:{}",plateId);
        int delete = blogPlateMapper.deleteByPrimaryKey(plateId);
        if (delete>0){
            return delete;
        }else {
          throw new GlobalException(CodeMsg.PLATE_INFO_ERROR);
        }
    }
}
